#include "ActionFactory.h"
#include "Machine.h"
#include "Tags.h"

ActionFactory::ActionFactory(Machine* machine) : _machine(machine)
{
    ptrType.insert("addchar", &ActionFactory::Addchar);
    ptrType.insert("timeout", &ActionFactory::Timeout);
    ptrType.insert("reset", &ActionFactory::Reset);
    ptrType.insert("send", &ActionFactory::Send);
    ptrType.insert("log", &ActionFactory::Log);

    //crc
    ptrType.insert("XOR-8bit", &ActionFactory::XOR8bit);
}

ActionPtr ActionFactory::MakeAction(const QString& type, const StringMap& properties)
{
    ActionPtr action(ptrType.contains(type) ? (this->*ptrType[type])() : 0);
    
    if (action) 
    {
        QString objectName = properties[tag::AObject];
        action->SetMachine(_machine);
        action->SetObject(objectFactory.GetObject(objectName, GetObjectValue(objectName)));
        action->SetSpecific(properties);
    }

    return action;
}

const QByteArray ActionFactory::GetObjectValue(const QString& object)
{
    return _machine && _machine->Globals().contains(object) ? _machine->Globals()[object] : object.toAscii();
}

Action* ActionFactory::Reset()
{
    return new ResetAction;
}

Action* ActionFactory::Send()
{
    return new SendAction;
}

Action* ActionFactory::Addchar()
{
    return new AddCharAction;
}

Action* ActionFactory::Timeout()
{
    return new TimeoutAction;
}

Action* ActionFactory::Log()
{
    return new LogAction;
}

Action* ActionFactory::XOR8bit()
{
    return new CrcAction;
}

